package fyp.spedmew.backend.service;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Handle file upload from android client
 */
public class GetUploadURLServlet extends HttpServlet{
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    private static final Logger log = Logger.getLogger(GetUploadURLServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        String blobUploadUrl = blobstoreService.createUploadUrl("/upload");
        blobUploadUrl = blobUploadUrl.replace("localhost","10.0.2.2");
        log.info("Return upload URL " + blobUploadUrl);
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.setContentType("text/plain");

        PrintWriter out = resp.getWriter();
        out.print(blobUploadUrl);
    }
}
